package cn.kk.day02zuoye.controller;
/*
 *@author KK on 2022.11.22 20:46
 */

import cn.kk.day02zuoye.dao.UserDao;
import cn.kk.day02zuoye.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/DelServlet")
public class DelServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("name");
        List<User> users = new UserDao().getUsers();
        User user1 = null;
        for (User user : users) {
            if (user.getName().equals(username)){
                user1 = user;
            }
        }
        users.remove(user1);
        request.setAttribute("users",users);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
